package com.joehongfa.webmagic;

import org.json.JSONArray;
import org.json.JSONObject;

import com.joehongfa.util.SaveXZCFToDb;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * 对爬到的数据处理
 * @author zhf
 *
 */
public class CustomerPipeline implements Pipeline {

	@Override
	public void process(ResultItems resultItems, Task task) {
		System.out.println("get page: " + resultItems.getRequest().getUrl());
		// 遍历所有结果，输出到控制台，其结果则是对应的value
		// for (Entry<String, Object> entry : resultItems.getAll().entrySet()) {
		// System.out.println(entry.getKey() + ":\t" + entry.getValue());

		// }

		String string = resultItems.get("jsonData");
		String orgName = resultItems.get("orgName");

		JSONArray jsonArray = new JSONObject(string).getJSONArray("list");// 将"list"抽取成jsonArray
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			if (jsonObject.getString("type").equals("02")) {// 02为行政处罚
				Object[] arrayXzcf = new String[6];// 拼凑数组
				// System.out.println(jsonObject.get("basis"));
				arrayXzcf[0] = jsonObject.get("punishedBehavior").toString();
				arrayXzcf[1] = jsonObject.get("punishedCategory").toString();
				arrayXzcf[2] = jsonObject.get("basis").toString();
				arrayXzcf[3] = jsonObject.get("dutyItem").toString();
				arrayXzcf[4] = jsonObject.get("remark").toString();
				arrayXzcf[5] = orgName;
				SaveXZCFToDb.save(arrayXzcf);
			}
		}
	}

}
